package com.wlz.ota.packageOriginal;

import com.wlz.ota.entity.PackageControllerFileVo;
import com.wlz.ota.entity.PackageControllerVo;
import com.wlz.ota.entity.PartTypeEntity;
import com.wlz.ota.entity.UploadFileEntity;
import com.wlz.ota.util.BusinessException;
import com.wlz.ota.util.CommonUtil;
import com.wlz.ota.util.PartTypeUtils;
import com.wlz.ota.util.ZipCompressUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 *  原始包
 * @author wlz
 * @date 2022-10-31  3:47 下午
 */
@Slf4j
public class OriginalPackageTest {

    private static final  String ZIP = "zip";

    private static final  String XML = "xml";

    PartTypeUtils partTypeUtils = new PartTypeUtils();

    public static void main(String[] args) {
        List<String> errorList = new ArrayList<>();
        String fileUrl = "/Users/wlz/Desktop/ESC008.zip";
        UploadFileEntity uploadFileEntity = uploadFile(fileUrl);
        System.out.println("uploadFileEntity {}" + uploadFileEntity);

        // 解析原始包
        List<PackageControllerVo> packageControllerVos = parseOriginalPackage(uploadFileEntity, errorList);
        if (null == packageControllerVos || packageControllerVos.size() <= 0) {
            errorList.add(String.format("包 【%s 不存在控制器包", uploadFileEntity.getFileOriginalName()));
            printResult(errorList);
            return;
        }
        // 解析控制器包
        parseControllerPackage(packageControllerVos, uploadFileEntity, errorList);

    }

    /**
     *  解析控制器包
     * @param packageControllerVos
     */
    private static void parseControllerPackage(List<PackageControllerVo> packageControllerVos, UploadFileEntity uploadFileEntity,List<String> errorList) {
        String[] split = BusinessConfigProperties.unembed.split(",");
        List<String> unembed = Arrays.asList(split);
        for (PackageControllerVo packageController : packageControllerVos) {
            List<File> fileList = new ArrayList<>();
            // 检查 控制器包 文件是否存在
            checkControllerPackageFile(packageController, uploadFileEntity,fileList, errorList);
            if (errorList.size() > 0) {
                printResult(errorList);
                return;
            }
            // 解析控制器包 内的文件
            for (File file : fileList) {
                PackageControllerFileVo packageControllerFileInfo = getPackageControllerFileVo(file);
                //校验控制器文件名称 必须为  xx-xxx.xx, 例如: 1-xxxx.aa、2-xxxx.bb 等
                String regex = "^([0-9]{1,2})-(.+)(\\.[A-Za-z0-9]+)$";
                if (!CommonUtil.checkRegular(packageControllerFileInfo.getFileFullName(), regex)) {
                    packageControllerFileInfo.setSuccess(false);
                    errorList.add(String.format("包【%s】控制器包【%s】中文件【%s】不符合命名规则,或者后缀错误", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName(), packageControllerFileInfo.getFileFullName()));
                    printResult(errorList);
                    return;
                }
                packageControllerFileInfo.setFileType(packageControllerFileInfo.getFileName().split("-")[0]);
                packageControllerFileInfo.setFileRealName(packageControllerFileInfo.getFileName().substring(packageControllerFileInfo.getFileType().length() + 1));
                // 全量包
                if (0 == packageController.getPackageType()) {
                    //非嵌入式全量包(车机、tbox 包等)
                    if (unembed.contains(packageController.getPackageName())) {
                        if ("2".equals(packageControllerFileInfo.getFileType())) {
                            check2AppFile(uploadFileEntity, packageController, packageControllerFileInfo, errorList);
                            if (errorList.size()> 0) {
                                printResult(errorList);
                                return;
                            }
                            if (packageControllerFileInfo.isSuccess()) {
                                packageController.getPackageControllerFileList().add(packageControllerFileInfo);
                            }
                        }
                        else {
                            if (!"3".equals(packageControllerFileInfo.getFileType())) {
                                packageControllerFileInfo.setSuccess(false);
                                errorList.add(String.format("包【%s】控制器包【%s】中文件【%s】不满足规则,不存在此标号文件", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName(), packageControllerFileInfo.getFileFullName()));
                                printResult(errorList);
                                return;
                            }
                            if (packageControllerFileInfo.isSuccess()) {
                                packageController.getPackageControllerFileList().add(packageControllerFileInfo);
                            }
                        }
                    }
                    // 普通的全量包
                    else {
                        if (packageControllerFileInfo.getFileType().equals("1")) {
                            if (CommonUtil.checkRegular(packageControllerFileInfo.getFileName(), BusinessConfigProperties.driveFileExp)) {
                                Pattern pattern = Pattern.compile(BusinessConfigProperties.driveFileExp);
                                Matcher matcher = pattern.matcher(packageControllerFileInfo.getFileName());
                                while (matcher.find()) {
                                    if (!matcher.group(2).equals(packageController.getPartName())) {
                                        packageControllerFileInfo.setSuccess(false);
                                        errorList.add(String.format("包【%s】控制器包【%s】中文件【%s】零部件名称与控制器包不同", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName(), packageControllerFileInfo.getFileFullName()));
                                        printResult(errorList);
                                        return;
                                    }
                                }
                            } else {
                                packageControllerFileInfo.setSuccess(false);
                                errorList.add(String.format("包【%s】控制器包【%s】中文件【%s】文件名称错误", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName(), packageControllerFileInfo.getFileFullName()));
                                printResult(errorList);
                                return;
                            }
                            if (packageControllerFileInfo.isSuccess()) {
                                packageController.getPackageControllerFileList().add(packageControllerFileInfo);
                            }
                        }
                        else if ("2".equals(packageControllerFileInfo.getFileType())) {
                            check2AppFile(uploadFileEntity, packageController, packageControllerFileInfo, errorList);
                            if (errorList.size()> 0) {
                                printResult(errorList);
                                return;
                            }
                            if (packageControllerFileInfo.isSuccess()) {
                                packageController.getPackageControllerFileList().add(packageControllerFileInfo);
                            }
                        }
                        else if ("7".equals(packageControllerFileInfo.getFileType())) {
                            if (CommonUtil.checkRegular(packageControllerFileInfo.getFileRealName(),BusinessConfigProperties.markFileExp)) {
                                Pattern pattern = Pattern.compile(BusinessConfigProperties.markFileExp);
                                Matcher matcher = pattern.matcher(packageControllerFileInfo.getFileRealName());
                                while (matcher.find()) {
                                    if (!matcher.group(1).equals(packageController.getPartName())) {
                                        packageControllerFileInfo.setSuccess(false);
                                        errorList.add(String.format("包【%s】控制器包【%s】中文件【%s】零部件名称与控制器包不同", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName(), packageControllerFileInfo.getFileFullName()));
                                        printResult(errorList);
                                        return;
                                    }
                                    if (!matcher.group(3).equals(packageController.getSoftwareVersion())) {
                                        packageControllerFileInfo.setSuccess(false);
                                        errorList.add(String.format("包【%s】控制器包【%s】中文件【%s】标定版本与控制器包不同", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName(), packageControllerFileInfo.getFileFullName()));
                                        printResult(errorList);
                                        return;
                                    }
                                }
                            }else {
                                packageControllerFileInfo.setSuccess(false);
                                errorList.add(String.format("包【%s】控制器包【%s】中文件【%s】文件名称错误", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName(), packageControllerFileInfo.getFileFullName()));
                                printResult(errorList);
                                return;
                            }
                            if (packageControllerFileInfo.isSuccess()) {
                                packageController.getPackageControllerFileList().add(packageControllerFileInfo);
                            }
                        }
                        else {
                            if (packageControllerFileInfo.isSuccess()) {
                                packageController.getPackageControllerFileList().add(packageControllerFileInfo);
                            }
                        }
                    }
                }
                // 差分包
                else {
                    if ("2".equals(packageControllerFileInfo.getFileType())) {
                        check2AppFile(uploadFileEntity, packageController, packageControllerFileInfo, errorList);
                        if (errorList.size()> 0) {
                            printResult(errorList);
                            return;
                        }
                        if (packageControllerFileInfo.isSuccess()) {
                            packageController.getPackageControllerFileList().add(packageControllerFileInfo);
                        }
                    }
                    else {
                        if (!"3".equals(packageControllerFileInfo.getFileType())) {
                            packageControllerFileInfo.setSuccess(false);
                            errorList.add(String.format("包【%s】控制器包【%s】中文件【%s】不满足规则,不存在此标号文件", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName(), packageControllerFileInfo.getFileFullName()));
                            printResult(errorList);
                        }
                        if (packageControllerFileInfo.isSuccess()) {
                            packageController.getPackageControllerFileList().add(packageControllerFileInfo);
                        }
                    }
                }
            }

            //校验控制器文件
            if (packageController.getPackageControllerFileList().isEmpty()) {
                errorList.add(String.format("包【%s】控制器包【%s】中不存在符合命名规则的文件", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName()));
                printResult(errorList);
                return;
            }
            if (unembed.contains(packageController.getPartName())) {
                long twoFileCount = packageController.getPackageControllerFileList().stream().filter(f -> "2".equals(f.getFileType())).count();
                //非嵌入式包文件校验
                if (twoFileCount <= 0) {
                    errorList.add(String.format("包【%s】控制器包【%s】中不存在符合规则的2-文件", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName()));
                    printResult(errorList);
                    return;
                }
            }
            else {
                long oneFileCount = packageController.getPackageControllerFileList().stream().filter(f -> f.getFileType().equals("1")).count();
                long twoFileCount = packageController.getPackageControllerFileList().stream().filter(f -> f.getFileType().equals("2")).count();
                long threeFileCount = packageController.getPackageControllerFileList().stream().filter(f -> f.getFileType().equals("3") && f.getFileSuffix().equals(XML)).count();
                long sevenFileCount = packageController.getPackageControllerFileList().stream().filter(f -> f.getFileType().equals("7")).count();
                long nineFileCount = packageController.getPackageControllerFileList().stream().filter(f -> f.getFileType().equals("9")).count();
                //非嵌入式包文件校验
                if (oneFileCount <= 0) {
                    errorList.add(String.format("包【%s】控制器包【%s】中不存在符合规则的1-文件", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName()));
                    printResult(errorList);
                    return;
                }
                if (threeFileCount <= 0) {
                    errorList.add(String.format("包【%s】控制器包【%s】中不存在符合规则的3-文件", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName()));
                    printResult(errorList);
                    return;
                }
                if (nineFileCount <= 0) {
                    errorList.add(String.format("包【%s】控制器包【%s】中不存在符合规则的9-文件", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName()));
                    printResult(errorList);
                    return;
                }
                if (twoFileCount <= 0) {
                    errorList.add(String.format("包【%s】控制器包【%s】中不存在符合规则的2-文件", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName()));
                    printResult(errorList);
                    return;
                }else {
                    if (twoFileCount > 1) {
                        List<PackageControllerFileVo> twoFileList = packageController.getPackageControllerFileList().stream().filter(p -> p.getFileType().equals("2")).sorted(Comparator.comparing(PackageControllerFileVo::getFileFullName)).collect(Collectors.toList());
                        for (int i = 0; i < twoFileList.size(); i++) {
                            try {
                                String serialNumber = twoFileList.get(i).getFileRealName().substring(19, 22);
                                String number = serialNumber.substring(1);
                                Integer boxNumber = Integer.parseInt(number);
                                if (!boxNumber.equals(i + 1)) {
                                    errorList.add(String.format("包【%s】控制器包【%s】中2-文件序号错误", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName()));
                                    printResult(errorList);
                                    return;
                                }
                            } catch (Exception e) {
                                log.error(e.toString());
                                errorList.add(String.format("包【%s】控制器包【%s】中2-文件序号错误", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName()));
                                printResult(errorList);
                                return;
                            }
                        }
                    }
                }
                if (packageController.getIsCalibration().equals(1)) {
                    if (sevenFileCount <= 0) {
                        errorList.add(String.format("包【%s】控制器包【%s】中不存在符合规则的7-文件", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName()));
                        printResult(errorList);
                        return;
                    } else {
                        if (sevenFileCount > 1) {
                            List<PackageControllerFileVo> sevenFileList = packageController.getPackageControllerFileList().stream().filter(p -> p.getFileType().equals("7")).sorted(Comparator.comparing(PackageControllerFileVo::getFileFullName)).collect(Collectors.toList());
                            for (int i = 0; i < sevenFileList.size(); i++) {
                                try {
                                    String serialNumber = sevenFileList.get(i).getFileRealName().substring(19, 22);
                                    String number = serialNumber.substring(1);
                                    Integer boxNumber = Integer.parseInt(number);
                                    if (!boxNumber.equals(i + 1)) {
                                        errorList.add(String.format("包【%s】控制器包【%s】中7-文件序号错误", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName()));
                                        printResult(errorList);
                                        return;
                                    }
                                } catch (Exception e) {
                                    log.error(e.toString());
                                    errorList.add(String.format("包【%s】控制器包【%s】中7-文件序号错误", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName()));
                                    printResult(errorList);
                                    return;
                                }
                            }
                        }
                    }
                } else {
                    if (sevenFileCount > 0) {
                        errorList.add(String.format("包【%s】控制器包【%s】中不应该含有标定文件", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName()));
                        printResult(errorList);
                        return;
                    }
                }
            }
        }
    }


    /**
     *  检查控制器文件
     * @param packageController
     * @param uploadFileEntity
     * @param errorList
     */
    private static void checkControllerPackageFile(PackageControllerVo packageController, UploadFileEntity uploadFileEntity , List<File> fileList, List<String> errorList) {
        String unPackageControllerPath = packageController.getFullPath().substring(0, packageController.getFullPath().lastIndexOf("."));
        File unzip = null;
        try {
            unzip = ZipCompressUtil.unZip4j(packageController.getFullPath(), unPackageControllerPath, Charset.forName("GBK"));
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(String.format("包【%s】控制器包【%s】解压失败", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName()));
        }
        if (unzip == null) {
            errorList.add(String.format("包【%s】控制器包【%s】解压失败", uploadFileEntity.getFileOriginalName()));
            return;
        }
        //获取控制器包里的文件
        File unZipFile = new File(unPackageControllerPath);
        List<File> unZipFileList = Arrays.asList(unZipFile.listFiles());
        if (unZipFileList.isEmpty()) {
            errorList.add(String.format("包【%s】控制器包【%s】中没有文件", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName()));
            return;
        }
        //控制器包里不能有文件夹
        List<String> dirList = unZipFileList.stream().filter(p -> p.isDirectory()).map(File::getName).collect(Collectors.toList());
        if (!dirList.isEmpty()) {
            errorList.add(String.format("包【%s】控制器包【%s】中不能有文件夹,这些文件夹有:【%s】", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName()));
            return;
        }
        //控制器包里没有文件
        fileList.addAll(unZipFileList.stream().filter(p -> p.isFile()).collect(Collectors.toList()));
        if (fileList.isEmpty() && fileList.size() <= 0) {
            errorList.add(String.format("包【%s】控制器包【%s】中不存在控制器文件", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName()));
            return;
        }

    }

    /**
     *  校验 2- 文件
     * @param packageController
     * @param packageControllerFileInfo
     */
    private static void check2AppFile(UploadFileEntity uploadFileEntity,PackageControllerVo packageController, PackageControllerFileVo packageControllerFileInfo,
                                      List<String> errorList) {
        if (CommonUtil.checkRegular(packageControllerFileInfo.getFileRealName(),BusinessConfigProperties.appFileExp)) {
            Pattern pattern = Pattern.compile(BusinessConfigProperties.appFileExp);
            Matcher matcher = pattern.matcher(packageControllerFileInfo.getFileRealName());
            while (matcher.find()) {
                if (!matcher.group(1).equals(packageController.getPartName())) {
                    packageControllerFileInfo.setSuccess(false);
                    errorList.add(String.format("包【%s】控制器包【%s】中文件【%s】零部件名称与控制器包不同", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName(), packageControllerFileInfo.getFileFullName()));
                }
                if (!matcher.group(3).equals(packageController.getSoftwareVersion())) {
                    packageControllerFileInfo.setSuccess(false);
                    errorList.add(String.format("包【%s】控制器包【%s】中文件【%s】软件版本与控制器包不同", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName(), packageControllerFileInfo.getFileFullName()));
                }
            }
        }else {
            packageControllerFileInfo.setSuccess(false);
            errorList.add(String.format("包【%s】控制器包【%s】中文件【%s】文件名称错误", uploadFileEntity.getFileOriginalName(), packageController.getPackageFullName(), packageControllerFileInfo.getFileFullName()));
        }
    }

    /**
     *  解析 原始包
     * @param uploadFileEntity
     * @param errorList
     */
    private static List<PackageControllerVo> parseOriginalPackage(UploadFileEntity uploadFileEntity,List<String> errorList) {
        // 校验 原始包
        List<File> fileList = new ArrayList<>();
        // 检查原始包 文件 是否符合要求
        checkOriginalPackageFile(uploadFileEntity, fileList, errorList);
        if (errorList.size() > 0) {
            return null;
        }
        List<PackageControllerVo> packageControllerVos = new ArrayList<>();
        for (File file : fileList) {
            PackageControllerVo packageControllerVo = getPackageControllerVo(file);
            // 解析包名, 判断 是全量包还是差分包 (控制器包类型(0:全量包 1:差分包))
            parsePackageName(packageControllerVo);
            if (null == packageControllerVo.getPackageType()) {
                errorList.add(String.format("包【%s】中控制器包【%s】名称错误,，正确格式：控制器（三位大写英文字母）+控制器号（大写字母A-Z和数字0-9组成，9个字符长度）+软版本" +
                        "（含标定：S开头+三位数字的主软件版本+三位数字次软件版本+C开头+三位数字的主软件版本+三位数字次软件版本；" +
                        "不含标定：S开头+三位数字的主软件版本+三位数字次软件版本）", uploadFileEntity.getFileOriginalName(), packageControllerVo.getPackageFullName()));
                return null;
            }
            // 获取 控制器类型列表(读取excel)
            PartTypeEntity partType = PartTypeUtils.getPartType(packageControllerVo.getPartName());
            packageControllerVo.setIsCalibration(partType.getIsCalibration());
            // 控制器包类型(0:全量包 1:差分包)
            if (packageControllerVo.getPackageType() == 0) {
                int t = packageControllerVo.getIsCalibration() == 0 ? 7 : 14;
                int length = packageControllerVo.getSoftwareVersion().length();
                if (length != t) {
                    if (t == 7) {
                        errorList.add(String.format("包【%s】中控制器包【%s】不能含标定 ", uploadFileEntity.getFileOriginalName(), packageControllerVo.getPackageFullName()));
                        return null;
                    } else if (t == 14) {
                        errorList.add(String.format("包【%s】中控制器包【%s】必须含标定 ", uploadFileEntity.getFileOriginalName(), packageControllerVo.getPackageFullName()));
                        return null;
                    }
                }else {
                    if (t == 14) {
                        packageControllerVo.setCalibrationVersion(packageControllerVo.getSoftwareVersion().substring(7));
                        packageControllerVo.setSoftwareVersion(packageControllerVo.getSoftwareVersion().substring(0, 7));
                    }
                }
            }
            //校验控制器包是否在数据库中存在
            //todo

            // 返回
            packageControllerVos.add(packageControllerVo);
        }
        return packageControllerVos;
    }

    /**
     *  检查 原始包 文件
     * @param uploadFileEntity
     * @param fileList
     * @param errorList
     */
    private static void checkOriginalPackageFile(UploadFileEntity uploadFileEntity, List<File> fileList, List<String> errorList) {
        if (!uploadFileEntity.getFileSuffix().equalsIgnoreCase(ZIP)) {
            errorList.add(String.format("包【%s】的后缀必须是.zip", uploadFileEntity.getFileOriginalName()));
            return;
        }
        // 解压 原始包
        String unPackageOriginalPath = uploadFileEntity.getFullPath().substring(0, uploadFileEntity.getFullPath().lastIndexOf("."));
        File unzip = null;
        try {
            unzip = ZipCompressUtil.unZip4j(uploadFileEntity.getFullPath(), unPackageOriginalPath, Charset.forName("GBK"));
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(String.format("包【%s】解压失败", uploadFileEntity.getFileOriginalName()));
        }

        if (null == unzip) {
            errorList.add(String.format("包【%s】解压失败", uploadFileEntity.getFileOriginalName()));
            return;
        }
        List<File> unZipFileList = Arrays.asList(unzip.listFiles());
        if (unZipFileList.isEmpty()) {
            errorList.add(String.format("包【%s】中没有文件", uploadFileEntity.getFileOriginalName()));
            return;
        }
        List<String> dirList = unZipFileList.stream().filter(p -> p.isDirectory()).map(File::getName).collect(Collectors.toList());
        if (!dirList.isEmpty()) {
            errorList.add(String.format("包【%s】中不能有文件夹,这些文件夹有:【%s】", uploadFileEntity.getFileOriginalName(),String.join(",", dirList)));
            return;
        }
        List<String> failedList = unZipFileList.stream().filter(p -> p.isFile() && !p.getName().endsWith("." + ZIP)).map(File::getName).collect(Collectors.toList());
        if (!failedList.isEmpty()) {
            errorList.add(String.format("包【%s】中的文件后缀不是.zip,这些文件有:【%s】", uploadFileEntity.getFileOriginalName(),String.join(",", failedList)));
            return;

        }
        fileList.addAll(unZipFileList.stream().filter(p -> p.isFile() && p.getName().endsWith("." + ZIP)).collect(Collectors.toList()));
        if (fileList.isEmpty() && fileList.size() <= 0) {
            errorList.add(String.format("包【%s】中不存在.zip文件 ", uploadFileEntity.getFileOriginalName()));
            return;
        }
    }

    public static UploadFileEntity uploadFile(String fileUrl) {
        File file = new File(fileUrl);
        UploadFileEntity uploadFileEntity = new UploadFileEntity();
        uploadFileEntity.setUploadFileId(UUID.randomUUID().toString());
        uploadFileEntity.setFileOriginalName(file.getName());
        uploadFileEntity.setFileSuffix(file.getName().substring(file.getName().lastIndexOf(".") + 1));
        uploadFileEntity.setFileSize((double) file.length());
        uploadFileEntity.setFileMd5("");
        uploadFileEntity.setStatus(0);
        uploadFileEntity.setFullPath(fileUrl);
        System.out.println("file: {} "+  file);
        System.out.println("fileSize: {}" +  getPrintSize(file.length()));
        return uploadFileEntity;
    }

    public static String getPrintSize(long size) {
        // 如果字节数少于1024，则直接以B为单位，否则先除于1024，后3位因太少无意义
        double value = (double) size;
        if (value < 1024) {
            return String.valueOf(value) + "B";
        } else {
            value = new BigDecimal(value / 1024).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
        }
        // 如果原字节数除于1024之后，少于1024，则可以直接以KB作为单位
        // 因为还没有到达要使用另一个单位的时候
        // 接下去以此类推
        if (value < 1024) {
            return String.valueOf(value) + "KB";
        } else {
            value = new BigDecimal(value / 1024).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
        }
        if (value < 1024) {
            return String.valueOf(value) + "MB";
        } else {
            // 否则如果要以GB为单位的，先除于1024再作同样的处理
            value = new BigDecimal(value / 1024).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
            return String.valueOf(value) + "GB";
        }
    }

    public static void printResult(List<String> errorList) {
        System.out.println("===================================");
        for (String error : errorList) {
            System.out.println(error);
        }
    }

    public static PackageControllerFileVo getPackageControllerFileVo(File file) {
        PackageControllerFileVo packageControllerFileInfo = new PackageControllerFileVo();
        packageControllerFileInfo.setSuccess(true);
        packageControllerFileInfo.setFullPath(file.getAbsolutePath().replace('\\', '/'));
        if (file.getName().lastIndexOf(".") == -1) {
            packageControllerFileInfo.setFileName(file.getName());
        } else {
            packageControllerFileInfo.setFileName(file.getName().substring(0, file.getName().lastIndexOf(".")));
        }
        packageControllerFileInfo.setFileFullName(file.getName());
        packageControllerFileInfo.setFileSuffix(file.getName().substring(file.getName().lastIndexOf(".") + 1));
        packageControllerFileInfo.setFileSize(file.length());
        return packageControllerFileInfo;
    }

    public static PackageControllerVo getPackageControllerVo(File file) {
        PackageControllerVo packageControllerVo = new PackageControllerVo();
        packageControllerVo.setSuccess(true);
        packageControllerVo.setFullPath(file.getAbsolutePath().replace('\\', '/'));
        packageControllerVo.setPackageName(file.getName().substring(0, file.getName().lastIndexOf(".")));
        packageControllerVo.setPackageFullName(file.getName());
        packageControllerVo.setPackageSuffix(file.getName().substring(file.getName().lastIndexOf(".") + 1));
        packageControllerVo.setPackageSize(file.length());
        return packageControllerVo;
    }

    public static void parsePackageName(PackageControllerVo packageControllerVo) {
        String regex = BusinessConfigProperties.diffPackageExp;
        //校验规则1
        boolean flagRegular1 = CommonUtil.checkRegular(packageControllerVo.getPackageName(), regex);
        if (!flagRegular1) {
            //校验规则2
            regex = BusinessConfigProperties.fullPackageExp;
        }
        boolean flagRegular2 = CommonUtil.checkRegular(packageControllerVo.getPackageName(), regex);
        if (!flagRegular2) {
            return;
        } else {
            if (regex.equals(BusinessConfigProperties.diffPackageExp)) {
                // 差分包
                packageControllerVo.setPackageType(1);
                Pattern pattern = Pattern.compile(regex);
                Matcher matcher = pattern.matcher(packageControllerVo.getPackageName());
                while (matcher.find()) {
                    packageControllerVo.setPartName(matcher.group(1));
                    packageControllerVo.setPartNumber(matcher.group(2));
                    packageControllerVo.setTargetSoftwareVersion(matcher.group(3));
                    packageControllerVo.setOriginalSoftwareVersion(matcher.group(6));
                }
            } else {
                // 全量包
                packageControllerVo.setPackageType(0);
                Pattern pattern = Pattern.compile(regex);
                Matcher matcher = pattern.matcher(packageControllerVo.getPackageName());
                while (matcher.find()) {
                    packageControllerVo.setPartName(matcher.group(1));
                    packageControllerVo.setPartNumber(matcher.group(2));
                    packageControllerVo.setSoftwareVersion(matcher.group(3));
                }
            }
        }
    }

}
